/**
 入驻小区
 **/
(function (vc) {
    var DEFAULT_PAGE = 1;
    var DEFAULT_ROWS = 10;
    vc.extends({
        data: {
            downShopHouseOutManageInfo: {
                inoutOrderProducts: [],
                remark: '',
                orderType: '20000',
                orderId: '',
                totalMount: 0.00,
                relOrderId: '',
                shopId: vc.getCurrentCommunity().shopId
            }
        },
        _initMethod: function () {
        },
        _initEvent: function () {
            vc.on('downShopHouseOutManage', 'chooseInoutOrder', function (_product) {
                $that.downShopHouseOutManageInfo.orderId = _product.orderId;
                $that.downShopHouseOutManageInfo.relOrderId = _product.orderId;
                $that._listInoutOrderProduct()
            });
        },
        methods: {
            _doInoutOrder: function () {
                let _inoutOrderProducts = $that.downShopHouseOutManageInfo.inoutOrderProducts;
                if (_inoutOrderProducts.length < 1) {
                    vc.toast('请选择商品');
                    return;
                }
                if (!$that.downShopHouseOutManageInfo.remark) {
                    vc.toast('退货说明不能为空');
                    return;
                }
                _inoutOrderProducts.forEach(item => {
                    if (!item.count || item.count <= 0) {
                        vc.toast("请输入正确的数量");
                        throw Error();
                    }
                    if (parseInt(item.count) > parseInt(item.prodCount)) {
                        vc.toast("退货数量不能大于进库数量");
                        throw Error();
                    }
                });
                vc.http.apiPost(
                    '/inoutOrder/saveInoutOrder',
                    JSON.stringify($that.downShopHouseOutManageInfo),
                    {
                        emulateJSON: true
                    },
                    function (json, res) {
                        let _json = JSON.parse(json);
                        if (_json.code == 0) {
                            //关闭model
                            vc.toast(_json.msg);
                            vc.goBack();
                            return;
                        }
                        vc.toast(_json.msg);
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                        vc.toast(errInfo);
                    });
            },
            _openAddShopHouseModal: function () {
                vc.emit('chooseInoutOrder', 'openChooseInoutOrderModel', {
                    state: '8008'
                });
            },
            _listInoutOrderProduct: function () {
                let param = {
                    params: {
                        page: 1,
                        row: 50,
                        orderId: $that.downShopHouseOutManageInfo.orderId,
                        shopId: vc.getCurrentCommunity().shopId
                    }
                };
                //发送get请求
                vc.http.apiGet('/inoutOrder.queryInoutOrderProduct',
                    param,
                    function (json, res) {
                        let _json = JSON.parse(json);
                        $that.downShopHouseOutManageInfo.inoutOrderProducts = _json.data;
                    }, function (errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
            _goBack: function () {
                vc.goBack();
            },
            _deleteProduct: function (_inoutOrderProduct) {
                let _inoutOrderProducts = $that.downShopHouseOutManageInfo.inoutOrderProducts;
                let index = _inoutOrderProducts.indexOf(_inoutOrderProduct);
                if (index > -1) {
                    _inoutOrderProducts.splice(index, 1);
                }
            },
            _caculTotalMount: function () {
                let j = 0;
                $that.downShopHouseOutManageInfo.inoutOrderProducts.forEach(item => {
                    if (!isNaN(item.count) && !isNaN(item.price)) {
                        j += parseFloat(item.count) * parseFloat(item.price).toFixed(2);
                    } else {
                        $that.downShopHouseOutManageInfo.totalMount = "请输入正确的数字";
                        throw Error();
                    }
                });
                $that.downShopHouseOutManageInfo.totalMount = j.toFixed(2);
            }
        }
    });
})(window.vc);
